package com.codemonkey.lq.common.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;
import java.util.UUID;

public class StringHelper {
	private StringHelper() {
		throw new Error("Can not instantize ! ! !");
	}

	public static final String EMPTY = "";

	/**
	 * for test!! not call it !!
	 */
	@Deprecated
	public static String getSelfLocation() {
		// String path =
		// StringHelper.class.getProtectionDomain().getCodeSource().getLocation().toString();
		// String path = System.getProperty("user.dir");
		String path = StringHelper.class.getClassLoader().getResource("/").getPath();
		return path;
	}

	/**
	 * 在 param1指定的字符 的 param2指定的位置 插入 param3指定的字符
	 * 
	 * @param orginStr
	 * @param index
	 * @param strWillInsert
	 * @return
	 */
	public static String insertStr(String orginStr, int index, String strWillInsert) {
		String endStr = orginStr.substring(index);
		String startStr = orginStr.substring(0, index);
		return startStr + strWillInsert + endStr;
	}

	public static String getSpecialChar(String str, int index) {
		assert (index <= str.length() - 1);
		assert (index >= 0);

		return str.substring(index, index + 1);
	}

	// 生成UUID
	public static String NEWUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	public static String concat(String orgin, String appended) {
		return orgin + appended;
	}

	public static String concat(String orgin, String appended1, String appended2) {
		return orgin + appended1 + appended2;
	}

	public static InputStream String2InputStream(String str) {
		ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
		return stream;
	}

	/**
	 * 直接摘抄自org.springframework.util.StringUtils中的同名方法
	 * 
	 * @param str
	 * @param delimiters
	 * @return
	 */
	public static String[] tokenizeToStringArray(String str, String delimiters) {
		return tokenizeToStringArray(str, delimiters, true, true);
	}

	/**
	 * 直接摘抄自org.springframework.util.StringUtils中的同名方法
	 * 
	 * @param str
	 * @param delimiters
	 * @param trimTokens
	 * @param ignoreEmptyTokens
	 * @return
	 */
	public static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens,
			boolean ignoreEmptyTokens) {

		if (str == null) {
			return null;
		}
		StringTokenizer st = new StringTokenizer(str, delimiters);
		List<String> tokens = new ArrayList<String>();
		while (st.hasMoreTokens()) {
			String token = st.nextToken();
			if (trimTokens) {
				token = token.trim();
			}
			if (!ignoreEmptyTokens || token.length() > 0) {
				tokens.add(token);
			}
		}
		return toStringArray(tokens);
	}

	/**
	 * 直接摘抄自org.springframework.util.StringUtils中的同名方法
	 * 
	 * @param collection
	 * @return
	 */
	public static String[] toStringArray(Collection<String> collection) {
		if (collection == null) {
			return null;
		}
		return collection.toArray(new String[collection.size()]);
	}

	/**
	 * From apache.commons.lang3
	 * @param cs
	 * @return
	 */
	public static boolean isEmpty(final CharSequence cs) {
		return cs == null || cs.length() == 0;
	}

	/**
	 * From apache.commons.lang3
	 * @param cs
	 * @return
	 */
	public static boolean isNotEmpty(final CharSequence cs) {
		return !isEmpty(cs);
	}

	/**
	 * "file:/home/whf/cn/fh" -> "/home/whf/cn/fh"
	 * "jar:file:/home/whf/foo.jar!cn/fh" -> "/home/whf/foo.jar"
	 */
	public static String getRootPath(URL url) {
		String fileUrl = url.getFile();
		int pos = fileUrl.indexOf('!');

		if (-1 == pos) {
			return fileUrl;
		}

		return fileUrl.substring(5, pos);
	}

	/**
	 * "cn.fh.lightning" -> "cn/fh/lightning"
	 * @param name
	 * @return
	 */
	public static String dotToSplash(String name) {
		return name.replaceAll("\\.", "/");
	}

	/**
	 * "Apple.class" -> "Apple"
	 */
	public static String trimExtension(String name) {
		int pos = name.indexOf('.');
		if (-1 != pos) {
			return name.substring(0, pos);
		}

		return name;
	}

	/**
	 * /application/home -> /home
	 * @param uri
	 * @return
	 */
	public static String trimURI(String uri) {
		String trimmed = uri.substring(1);
		int splashIndex = trimmed.indexOf('/');

		return trimmed.substring(splashIndex);
	}
}
